#include "texture.h"

#include "textureMaterial.h"

#include <iostream>
#include <GL/glew.h>

TextureMaterial::TextureMaterial() : Material() {


}

TextureMaterial::TextureMaterial(nv::vec4f diffuseAmbient, nv::vec4f specular, float shinyness)
: Material(diffuseAmbient, specular, shinyness) {


}

TextureMaterial::~TextureMaterial(){

	for(int i = 0; i < (int)textures.size(); i++)
	{
		Texture::clearTexture(textures[i]);
	}

	textures.clear();

}

void TextureMaterial::begin()
{
	for(int i = 0; i < (int)textures.size(); i++)
	{
		glActiveTexture(GL_TEXTURE0 + i);
		glEnable(textures[i]->getTargetType());
		glBindTexture(textures[i]->getTargetType(), textures[i]->getId());


	}
}
void TextureMaterial::end()
{
	for(int i = 0; i < (int)textures.size(); i++)
	{
		glActiveTexture(GL_TEXTURE0 + i);
		glDisable(textures[i]->getTargetType());
		glBindTexture(GL_TEXTURE_2D, 0);
	}
}

void TextureMaterial::setUpShaderVariables(unsigned int program){

	for(int i = 0; i < (int)names.size(); i++)
	{

		if(names[i].length() > 0){
			int loc = glGetUniformLocation(program, names[i].c_str());
			if(loc != -1)
				glUniform1i( loc, i );

		}

	}
}


void TextureMaterial::addTexture(Texture *tex, std::string name){

	if(textures.size() < 8){
		textures.push_back(tex);
		names.push_back(name);
	}else{
		printf("Too many textures in one texture material.");
	}

}
